<!--#include file="functions.asp" -->

<% 
   'set cn=Server.CreateObject("ADODB.Connection")
   'cn.Open "MP2ENTTEST2", "MP2", "MP2"
   dim sql
   sql ="SELECT MAX(ROWID) FROM EXIHIST WHERE SEQNUM=" & Request("seqnum")
   set rsMax=cn.execute(sql)
   maxnum=rsMax(0)-1

   sql ="SELECT LOCATION, (SELECT LOCNAME=CASE WHEN LASTNAME IS NOT NULL THEN EMP.FIRSTNAME + ' '  + EMP.LASTNAME WHEN LASTNAME IS NULL THEN EMP.FIRSTNAME END FROM dbo.EMP WHERE EMP.EMPCODE=EXIHIST.LOCATION) AS LOCNAME FROM EXIHIST WHERE ROWID=" & maxnum
   set rsMax=cn.execute(sql)
   
   '---------------- update EXIREG
   sql = "UPDATE EXIREG SET "
   if (request("IsUc")="Y") then
     if (request("hf")="c") then
       sql = sql & " LOCATION='FINANCE'," 
       sql = sql & " USERCOORD=''," 
	 end if
	 if (request("hf")="e") then
       sql = sql & " LOCATION='" & rsMax("LOCNAME") &"'," 
       'sql = sql & " USERCOORD=''," 
	 end if
   else
     sql = sql & " LOCATION='" & request("UC")& "',"
   end if
   sql = sql & " UPDATEUSER='" & empCode & "',"
   sql = sql & " INVSTATUS='REJECTED',"
   if (request("Comments")<>"") then
    txComments = dblq(request("Comments1")) & Chr(13)
	txComments = txComments & ">" & dblq(UCase(UsrFullname)) & "(" & now &"):  " 
	txComments = txComments & dblq(request("Comments"))& Chr(13)
	sql = sql & " COMMENTS= '" & txComments & "',"
   end if
   'sql = sql & " COMMENTS='" & dblq(request("Comments"))&"',"
   sql = sql & " UPDATESTAMP=GETDATE()"
   sql = sql & " WHERE SEQNUM=" & request("seqnum")
   
   cn.execute(sql)
   
   '----------------- update EXIHIST
   if (request("IsUc")="N") then
      	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,UPDATEUSER,UPDATESTAMP)"
   		sql = sql & " VALUES ("
   		sql = sql & request("seqnum") & ",'"
   		sql = sql & "IRJ','"
   		sql = sql & empCode & "','"
   		sql = sql & Dept & "','"
   		sql = sql & usrName & "',"
   		sql = sql & "GETDATE() )"
   		cn.execute(sql)
		
		sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,UPDATEUSER,UPDATESTAMP)"
   		sql = sql & " VALUES ("
   		sql = sql & request("seqnum") & ",'"
   		sql = sql & "IRT','"
   		sql = sql & request("UC") & "','"
   		sql = sql & Dept & "','"
   		sql = sql & usrName & "',"
   		sql = sql & "GETDATE() )"
   		cn.execute(sql)
   else
   		sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,UPDATEUSER,UPDATESTAMP)"
   		sql = sql & " VALUES ("
   		sql = sql & request("seqnum") & ",'"
		if (request("hf")="c") then
   		  sql = sql & "IRB','"
    	  sql = sql & request("recBy") & "','"
   		  sql = sql & "FINANCE','"
		end if
		if (request("hf")="e") then
   		  sql = sql & "IRB','"
    	  sql = sql & rsMax("LOCNAME") & "','"
   		  sql = sql & Dept &"','"
		end if
   		sql = sql & usrName & "',"
   		sql = sql & "GETDATE() )"
   		cn.execute(sql)
   end if
	



   if (request("IsUc")="Y") then
      if (request("hf")="c") then
       sRecipients =GetLoginName(request("recBy"))
	  else
	   sRecipients =GetLoginName(rsMax("LOCNAME"))
	  end if
	  'sRecipients = "DKusnadi@Premier-Oil.com;BKoernianto@Premier-Oil.com"
   else
	  sRecipients = getloginname(request("UC"))
	  'sCCList = "MAgus@Premier-Oil.com"
   end if

    sDomainName = "@Premier-oil.com"
	EmailSubject = "Electronic Invoice Approval "  
	EmailBody = "<HTML><HEAD><TITLE></TITLE></HEAD><BODY>"
	EmailBody = EmailBody & "<FONT face=Arial size=2>Dear Sir/Madame, </font><BR><BR>"
	EmailBody = EmailBody & "<FONT face=Arial size=2>" & usrFullName & " has rejected the following invoice: </font><BR><BR>"
	EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
	EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
	if (request("IsUc")="Y") then
	  if (request("hf")="c") then
	  EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='"& theURL& "/Inv_Detail.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
	  else
	  EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='"& theURL& "/My_InvApproval.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
	  end if
	else
	  EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='"& theURL& "/My_InvApproval.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
	end if
	EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you </font><BR><BR>"
	EmailBody = EmailBody & "<FONT face=Arial size=2><b>" & usrFullName &"</b></font>"	
	
	EmailBody = EmailBody & "</BODY></HTML>"
	
	dim MyBody
	MyBody = ReformatText(EmailBody)
	SendCDOEmail sRecipients, sCCList, "", UsrName & sDomainName, EmailSubject, MyBody

'back to approval page

response.redirect "my_Invoice.asp"

%>
